package com.ibeeking.found.upms.b.rest.controller.v1;

import com.ibeeking.found.upms.b.rest.service.ITenantAuthRoleService;
import com.ibeeking.found.upms.service.common.bo.TenantAuthRoleUserBO;
import com.ibeeking.found.upms.service.common.param.TenantAuthRoleParam;
import com.ibeeking.found.upms.service.common.query.TenantAuthRolePageQuery;
import com.ibeeking.found.upms.service.common.vo.TenantAuthRolePageVO;
import com.ibeeking.found.upms.service.common.vo.TenantAuthRoleVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName RoleController控制器
 * @Description 商户租户角色管理
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/b/shopRole/v1")
public class TenantAuthRoleController {

    @Resource
    private ITenantAuthRoleService authRoleServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询店铺角色列表")
    @PostMapping(value = "/pageList")
    public Page<TenantAuthRolePageVO> pageList(@RequestBody TenantAuthRolePageQuery query) {
        return authRoleServiceImpl.pageList(query);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "新增租户店铺角色")
    @PostMapping(value = "/add")
    public Boolean add(@RequestBody @Validated(BaseValidationGroup.Insert.class) TenantAuthRoleParam param) {
        return authRoleServiceImpl.add(param);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改租户店铺角色")
    @PostMapping(value = "/modify")
    public Boolean modify(@RequestBody @Validated(BaseValidationGroup.Update.class) TenantAuthRoleParam param) {
        return authRoleServiceImpl.modify(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询租户店铺角色")
    @GetMapping(value = "/view/{id}")
    public TenantAuthRoleVO view(@PathVariable("id") Long id) {
        return authRoleServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "启用/禁用租户店铺角色")
    @GetMapping(value = "/changeStatus/{id}/{status}")
    public Boolean changeStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return authRoleServiceImpl.changeStatus(id, status);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除租户店铺角色")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return authRoleServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询用户id列表")
    @GetMapping(value = "/queryUserIdsByRoleId/{roleId}")
    public List<String> queryUserIdsByRoleId(@PathVariable("roleId") @NotNull(message = "roleId不能为空") Long roleId) {
        List<TenantAuthRoleUserBO> tenantAuthRoleUserBos = authRoleServiceImpl.queryUserIdsByRoleId(roleId);
        return tenantAuthRoleUserBos.stream().map(s -> String.valueOf(s.getTenantUserId())).distinct().collect(Collectors.toList());
    }
}
